package com.surface.hao.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * ���ݿ������
 * @author songhao
 * @CreateDate 2015-04-07
 */
public class DbUtils {
	
	private static final String driver = "com.mysql.jdbc.Driver";
	private static final String dbName = "hdfs";
	private static final String passwrod = "root";
	private static final String userName = "root";
	private static final String url = "jdbc:mysql://localhost:3306/" + dbName + "?useunicode=true&characterEncoding=utf8&zeroDateTimeBehavior=round";
	
	/**
	 * �õ�mysql����
	 * @return
	 */
	public static Connection getConn(){
		Connection conn = null;
		try{
			Class.forName(driver);
			conn = DriverManager.getConnection(url, userName,
	                passwrod);
	        return conn;
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * �ر����ݿ�����
	 * @param conn
	 * @param stmt
	 * @param rs
	 * @return 
	 */
	public static void closeAll(Connection conn,Statement stmt,ResultSet rs){
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				System.err.println("���ݿ�ر�ResultSetʧ����...");
			}
		}
		if(stmt != null){
			try {
				stmt.close();
			} catch (SQLException e) {
				System.err.println("���ݿ�ر�Statementʧ����...");
			}
		}
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				System.err.println("���ݿ�ر�Connectionʧ����...");
			}
		}
	}
}

